import UIKit

//deinit
struct Bank {
    static var coinsInBank = 10_000
    static func venderCoins( numberOfCoinsToVend : Int) -> Int {
        var numberOfCoin = min(numberOfCoinsToVend, coinsInBank)
        coinsInBank -= numberOfCoinsToVend
        return numberOfCoin
   
    }
    
    static func receiveCoins(coins: Int) {
        coinsInBank += coins
    }
}

class Player {
    var coinsInPurse:Int
    init(coins: Int) {
        self.coinsInPurse = Bank.venderCoins(numberOfCoinsToVend: coins)
    }
    
    func winCoin(coins:Int) {
        coinsInPurse += Bank.venderCoins(numberOfCoinsToVend: coins)
    }
    
    deinit {
        Bank.receiveCoins(coins: coinsInPurse)
    }
}

var playerOne: Player? = Player(coins: 100)
playerOne?.winCoin(coins: 2_000)
playerOne?.coinsInPurse
print("the player now has \(playerOne!.coinsInPurse)")
print("the bank only has \(Bank.coinsInBank) coins.")
playerOne = nil
print("the bank now has \(Bank.coinsInBank) coins.")



